package com.wn.ziroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.ziroom.entity.Order;
import com.wn.ziroom.mapper.OrderMapper;
import com.wn.ziroom.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private OrderMapper orderMapper;
    private RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setOrderMapper(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Override
    public int createNewOrder(long orderNumber, Integer userId,String provinces, String city, String area, String street, String village, String address, String productionId, BigDecimal total) {

        int i = orderMapper.insertNewOrder(orderNumber,userId,provinces,city,area,street,village,address,productionId,total);
        return 0;

    }

    @Override
    public void sendOrderNumberToQueue(String out_trade_no) {
        rabbitTemplate.convertAndSend("NORMAL_EXCHANGE","NORMAL_ROUTING",out_trade_no);
    }

    @Override
    public void changeProductionStateToTwo(String productionOrderNumber) {
        orderMapper.updateProState(productionOrderNumber);
    }

    @Override
    public Order findOrderByOrderNumber(String productionOrderNumber) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_number",productionOrderNumber);
        return orderMapper.selectOne(orderQueryWrapper);
    }

    @Override
    public String checkOrderState(String orderNumber) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_number",orderNumber);
        Order order = orderMapper.selectOne(orderQueryWrapper);
        Integer stat = order.getStat();
        return stat+"";
    }

    @Override
    public void changeOrderStateToThree(String orderNumber) {
        orderMapper.updateProStateToThree(orderNumber);
    }
}
